﻿using HomagChina.CCT.ViewModels;
using HomagChina.CCT.Views;
using HomagChina.CCT.DataTypeModel;
using HomagGroup.Base.UI;
using System.Collections.ObjectModel;
using System.Linq;

namespace HomagChina.CCT.Router
{
    public class RouterModel
    {
        public static MainWindowViewModel mainWindowViewModel = new MainWindowViewModel();

        public RouterModel()
        {
            MainWindow mainWindow = new MainWindow();
            mainWindow.DataContext = mainWindowViewModel;
            NavigateServices.Instance.NavigateTo("Home");
            mainWindow.Show();
        }
    }

    public class NavigateServices
    {
        public object CurrentViewModel;
        public RouterItemDataModel CurrentRouteInfo;

        private static NavigateServices instance;
        private MainWindowViewModel mainWindowViewModel = RouterModel.mainWindowViewModel;
        private static readonly object padlock = new object();

        public RouterItemDataModel UpdateBindDataContext(string ID)
        {
            RouterItemDataModel newItem = null;
            switch (ID)
            {
                case "Home":
                    newItem = new HomeRoute().HomeRouteModel;
                    break;

                case "IntelligencePlatform":
                    newItem = new IntelligencePlatformRoute().IntelligencePlatformRouteModel;
                    break;

                default:
                    break;
            }
            return newItem;
        }

        public void NavigateTo(string ID)
        {
            var routerInfo = UpdateBindDataContext(ID);
            CurrentViewModel = routerInfo.ViewModel;
            routerInfo.userControl.DataContext = CurrentViewModel;
            routerInfo.ViewModel = CurrentViewModel;
            mainWindowViewModel.ContentElement = routerInfo.userControl;
            mainWindowViewModel.FunctionKeyBarMenu = routerInfo.functionKeyBarItems;
            mainWindowViewModel.RouterItems.Add(routerInfo);
            CurrentRouteInfo = routerInfo;
        }

        public void LanguageManagerCultureChanged(object sender, System.EventArgs e)
        {
            var UpdateRouters = new ObservableCollection<RouterItemDataModel>();
            foreach (var item in RouterModel.mainWindowViewModel.RouterItems)
            {
                UpdateRouters.Add(UpdateBindDataContext(item.ID));
            }
            mainWindowViewModel.RouterItems.Clear();
            mainWindowViewModel.RouterItems.AddRange(UpdateRouters);
            mainWindowViewModel.ContentElement = mainWindowViewModel.RouterItems.Last().userControl;
            mainWindowViewModel.FunctionKeyBarMenu = mainWindowViewModel.RouterItems.Last().functionKeyBarItems;
        }

        public static NavigateServices Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new NavigateServices();
                    }
                }
                return instance;
            }
        }
    }
}